package com.xinqi.modules.course.classmenu.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.enums.YesNoEnum;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.core.enums.ClassMenuHideEnum;
import com.xinqi.common.core.enums.CourseContentTypeEnum;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.course.classmenu.convert.ClassMenuConvert;
import com.xinqi.modules.course.classmenu.domain.ClassMenuEntity;
import com.xinqi.modules.course.classmenu.domain.MenuInitEntity;
import com.xinqi.modules.course.classmenu.dto.req.ClassMenuQueryDTO;
import com.xinqi.modules.course.classmenu.dto.rsp.ClassMenuResultDTO;
import com.xinqi.modules.course.classmenu.enums.MenuTypeEnum;
import com.xinqi.modules.course.classmenu.manager.ClassMenuManager;
import com.xinqi.modules.course.classmenu.mapper.ClassMenuInitMapper;
import com.xinqi.modules.course.classmenu.service.ClassMenuService;
import com.xinqi.modules.course.clazz.domain.ClassEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 课程中版块设置表(ClassMenu)表服务实现类
 *
 * @author liuxingxing
 * @since 2023-03-06 15:40:29
 */
@Service
@RequiredArgsConstructor
public class ClassMenuServiceImpl implements ClassMenuService {
    private final ClassMenuConvert classMenuConvert;
    private final ClassMenuManager classMenuManager;

    private final ClassMenuInitMapper classMenuInitMapper;

    /**
     * 创建课程，初始化课程菜单接口
     *
     * @param classId
     * @param courseId
     */
    @Override
    public void initClassMenu(Long classId, Long courseId) {
        List<MenuInitEntity> menuInitEntityList = classMenuInitMapper.selectList(null);
        if (CollectionUtil.isNotEmpty(menuInitEntityList)) {
            ArrayList<ClassMenuEntity> menuEntities = new ArrayList<>();
            for (MenuInitEntity menuInitEntity : menuInitEntityList) {
                ClassMenuEntity menuEntity = new ClassMenuEntity();
                menuEntity.setMenuInitId(menuInitEntity.getId());
                menuEntity.setClassId(classId);
                menuEntity.setCourseId(courseId);
                menuEntity.setOriginalTitle(menuInitEntity.getTitle());
                menuEntity.setIsDelete(0);
                menuEntity.setType(menuInitEntity.getType());
                menuEntity.setTitle(menuInitEntity.getTitle());
                menuEntity.setSort(menuInitEntity.getSort());
                menuEntity.setHiden(ClassMenuHideEnum.NO_HIDE.getHide());
                menuEntities.add(menuEntity);
            }
            classMenuManager.saveBatch(menuEntities);
        }
    }

    /**
     * 批量添加 菜单
     *
     * @param list
     */
    @Override
    public void initClassMenu(List<ClassEntity> list) {
        List<MenuInitEntity> menuInitEntityList = classMenuInitMapper.selectList(null);
        if (CollectionUtil.isNotEmpty(menuInitEntityList)) {
            ArrayList<ClassMenuEntity> menuEntities = new ArrayList<>();
            for (MenuInitEntity menuInitEntity : menuInitEntityList) {
                list.forEach(item -> {
                    ClassMenuEntity menuEntity = new ClassMenuEntity();
                    menuEntity.setMenuInitId(menuInitEntity.getId());
                    menuEntity.setClassId(item.getId());
                    menuEntity.setCourseId(item.getCourseId());
                    menuEntity.setOriginalTitle(menuInitEntity.getTitle());
                    menuEntity.setIsDelete(0);
                    menuEntity.setType(menuInitEntity.getType());
                    menuEntity.setTitle(menuInitEntity.getTitle());
                    menuEntity.setSort(menuInitEntity.getSort());
                    menuEntity.setHiden(ClassMenuHideEnum.NO_HIDE.getHide());
                    menuEntities.add(menuEntity);
                });
            }
            classMenuManager.saveBatch(menuEntities);
        }
    }


    /**
     * 根据班级ID获取菜单要显示内容
     */
    @Override
    public List<Integer> checkContentByMenu(Long classId) {
        List<Integer> contentTypes = new ArrayList<>();
        LambdaQueryWrapper<ClassMenuEntity> wrapper = new LambdaQueryWrapper();
        wrapper.eq(ClassMenuEntity::getClassId, classId);
        wrapper.eq(ClassMenuEntity::getHiden, YesNoNumberEnum.NO.getCode());
        List<ClassMenuEntity> menuList = classMenuManager.list(wrapper);
        List<Integer> type = menuList.stream().map(ClassMenuEntity::getType).collect(Collectors.toList());
        // 作业显示判断
        if (type.contains(MenuTypeEnum.HOMEWORK_TYPE.getCode())) {
            contentTypes.add(CourseContentTypeEnum.HOMEWORK.getCode());
        }
        // 测评显示判断
        if (type.contains(MenuTypeEnum.EXAM_TYPE.getCode())) {
            contentTypes.add(CourseContentTypeEnum.EXAM.getCode());
        }
        // 公告显示判断
        if (type.contains(MenuTypeEnum.NOTICE_TYPE.getCode())) {
            contentTypes.add(CourseContentTypeEnum.NOTICE.getCode());
        }
        // 讨论显示判断
        if (type.contains(MenuTypeEnum.DISCUSS_TYPE.getCode())) {
            contentTypes.add(CourseContentTypeEnum.DISCUSS.getCode());
        }
        // 资料库显示判断
        if (type.contains(MenuTypeEnum.DATABANK_TYPE.getCode())) {
            contentTypes.add(CourseContentTypeEnum.COURSEWARE.getCode());
            contentTypes.add(CourseContentTypeEnum.DATA.getCode());
            contentTypes.add(CourseContentTypeEnum.INTERACTIVE_VIDEO.getCode());
        }
        return contentTypes;
    }

    /**
     * 根据班级ID获取菜单要显示内容
     *
     * @param classId     班级ID
     * @param contentType 课程内容类型
     * @author dzy
     */
    @Override
    public void checkShowCourseContent(Long classId, CourseContentTypeEnum contentType) {

        // 业务不完善暂时去掉相关校验

        /*LambdaQueryWrapper<ClassMenuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClassMenuEntity::getClassId, classId);
        wrapper.eq(ClassMenuEntity::getHiden, YesNoNumberEnum.NO.getCode());
        List<ClassMenuEntity> menuList = classMenuManager.list(wrapper);
        List<Integer> types = menuList.stream().map(ClassMenuEntity::getType).collect(Collectors.toList());
        // 作业判断
        if (Objects.equals(CourseContentTypeEnum.HOMEWORK, contentType)) {
            CommonErrors.BAD_REQUEST.check(types.contains(MenuTypeEnum.HOMEWORK_TYPE.getCode())
                , MessageUtils.message("common.plate.remove.valid"));
        }
        // 资料库判断
        else if (Objects.equals(CourseContentTypeEnum.COURSEWARE, contentType)
            || Objects.equals(CourseContentTypeEnum.DATA, contentType)
            || Objects.equals(CourseContentTypeEnum.INTERACTIVE_VIDEO, contentType)) {
            CommonErrors.BAD_REQUEST.check(types.contains(MenuTypeEnum.DATABANK_TYPE.getCode())
                , MessageUtils.message("common.plate.remove.valid"));
        }
        // 测评判断
        else if (Objects.equals(CourseContentTypeEnum.EXAM, contentType)) {
            CommonErrors.BAD_REQUEST.check(types.contains(MenuTypeEnum.EXAM_TYPE.getCode())
                , MessageUtils.message("common.plate.remove.valid"));
        }
        // 公告判断
        else if (Objects.equals(CourseContentTypeEnum.NOTICE, contentType)) {
            CommonErrors.BAD_REQUEST.check(types.contains(MenuTypeEnum.NOTICE_TYPE.getCode())
                , MessageUtils.message("common.plate.remove.valid"));
        }
        // 讨论判断
        else if (Objects.equals(CourseContentTypeEnum.DISCUSS, contentType)) {
            CommonErrors.BAD_REQUEST.check(types.contains(MenuTypeEnum.DISCUSS_TYPE.getCode())
                , MessageUtils.message("common.plate.remove.valid"));
        }*/
    }

    @Override
    public List<ClassMenuResultDTO> findList(ClassMenuQueryDTO query) {
        return classMenuManager.list(wrapper(query)).stream()
            .map(classMenuConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<ClassMenuEntity> wrapper(ClassMenuQueryDTO query) {
        LambdaQueryWrapper<ClassMenuEntity> wrapper = Queries.lambda();
        Queries.accept(query.getClassId(), v -> wrapper.eq(ClassMenuEntity::getClassId, v));
        Queries.accept(query.getHidden(), v -> wrapper.eq(ClassMenuEntity::getHiden, v.getHide()));
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), query.getSort())) {
            wrapper.orderByDesc(ClassMenuEntity::getSort);
        }
        return wrapper;
    }
}
